package com.example.plant.util.images;

import static android.app.Activity.RESULT_OK;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


@SuppressWarnings("unused")
public class ResultContract extends ActivityResultContract<Bundle, Intent> {

    /**
     * 调用相机拍照操作
     */
    public static final int CAMERA_CAPTURE = 0;

    /**
     * 从相册选择照片
     */
    public static final int PICTURE_SELECT = 1;

    /**
     * 承接拍照结果的uri 标记
     */
    public static final String CAPTURE_URI = "uri";

    /**
     * 操作
     */
    private final int action;

    private Uri imageUri;

    public ResultContract(int action, Uri imageUri) {
        this.action = action;
        this.imageUri = imageUri;
    }

    public ResultContract(int action) {
        this.action = action;
    }

    @NonNull
    @Override
    public Intent createIntent(@NonNull Context context, Bundle input) {
        if (action == CAMERA_CAPTURE) {
            Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            camera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            return camera;
        } else if (action == PICTURE_SELECT) {
            Intent gallery = new Intent(Intent.ACTION_PICK);
            gallery.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            return gallery;
        } else {
            return new Intent();
        }
    }

    @Override
    public Intent parseResult(int resultCode, @Nullable Intent intent) {
        if (resultCode == RESULT_OK) {
            return intent;
        }
        return null;
    }
}